const express = require('express'); //引入express
const mongoose = require('mongoose');
const app = express();
const bodyParser = require('body-parser'); //post请求需要安装的模块
app.use(bodyParser.urlencoded({ limit: '500mb', extended: true }));
var session = require('express-session');
const cors = require('cors');

// websocket  1111111111
// const websocket = require('ws');
// (ws => {
// 	const server = new ws.Server({ port: 8090 });
// 	const init = () => {
// 		bindEven();
// 	};
// 	function bindEven() {
// 		server.on('open', handleOpen);
// 		server.on('close', handleclose);
// 		server.on('error', handleerror);
// 		server.on('connection', handleconnection);
// 	}
// 	function handleOpen() {
// 		console.log('websocket handleOpen');
// 	}
// 	function handleclose() {
// 		console.log('websocket handleclose');
// 	}
// 	function handleerror() {
// 		console.log('websocket handleerror');
// 	}
// 	function handleconnection(ws) {
// 		console.log('websocket handleconnection');
// 		ws.on('message', handleMessage);
// 	}
// 	function handleMessage(msg) {
// 		console.log('handleMessage', msg.toString());
// 		server.clients.forEach(e => {
// 			e.send(msg.toString());
// 		});
// 	}
// 	init();
// })(websocket);
// function useCors(req, res, next) {
// 	if (!req.get('Origin')) return next();
// 	// use "*" here to accept any origin
// 	res.set('Access-Control-Allow-Origin', req.headers.origin);
// 	res.set('Access-Control-Allow-Methods', '*');
// 	res.set('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
// 	res.header('Access-Control-Allow-Credentials', 'true');
// 	// res.set('Access-Control-Allow-Max-Age', 3600);
// 	if ('OPTIONS' === req.method) return res.sendStatus(200);
// 	next();
// }
app.use(cors()); //就这一部就已经解决了跨域
// app.use(useCors)
app.use(express.static('public')); //静态资源托管
const logAsync = require("./middleware/log-async");
app.use(logAsync()); //操作日志中间件

const users = require('./routes/api/users');
const role = require('./routes/api/role');
const menu = require('./routes/api/menu');
const operlog = require('./routes/api/operlog');
const dept = require('./routes/api/dept');
const profiles = require('./routes/api/profiles');
const upload = require('./routes/api/upload');
const captcha = require('./routes/api/captcha');
// 使用bodyParser中间件
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.use(
	session({
		secret: 'secret',
		resave: false,
		saveUninitalized: true,
		cookie: {
			maxAge: 1000 * 60
		}
	})
); //session缓存
app.use('/api/users', users); //用户账号管理
app.use('/api/role', role); //角色管理
app.use('/api/menu', menu); //菜单管理
app.use('/api/operlog', operlog); //日志
app.use('/api/dept', dept); //部门管理
app.use('/api/profiles', profiles);
app.use('/api/file', upload); //文件上传
app.use('/api/verify', captcha);
app.use('/api/article', require('./routes/api/article'));


// app.use(require('./middleware/error')); //引入使用错误处理中间件
// mongoose.connect('mongodb+srv://lsj:sr19970526@cluster0.kxjklwg.mongodb.net/?retryWrites=true&w=majority')
mongoose
	.connect(
		'mongodb+srv://admin:sr19970526@pro-first.hz7dj.mongodb.net/?retryWrites=true&w=majority'
)
	.then(() => {
		console.log('连接成功!');
	})
	.catch(error => {
		console.log('连接失败');
		console.log(error);
	});

// app.use(ctx=>{
//     ctx.body='hello word'
// })
app.get('/', (req, res) => {
	// console.log(req.query);
	// const { echostr } = req.query;
	// console.log(echostr);
	// req.body = echostr;
	// console.log(req.body);
	// console.log((res.body = echostr));
	// res.header("Access-Control-Allow-Origin", req.headers.origin);
	// const data= fs.sendFile(path.join(__dirname, 'assets/images/dw-1.jpg'));
	res.send('4444444444444444444444444555555555555555555555');
	// let path = 'assets/images/dw-1.jpg'
	// const data = fs.readFile(path, function (err, data) {
	//     console.log(data);
	//     if (err) {
	//       res.send('读取错误')
	//     } else {
	//       res.json({img:data})
	//     }
	//   })
});

app.use((req, res, next) => {
	console.log(req.query);
});

const port = process.env.PORT || 8060;
app.listen(port, () => {
	console.log(`Server runing ${port}`);
});
